package com.accloud.cloudservice;

import com.accloud.service.ACDeviceFind;
import com.accloud.service.Receiver;
import com.accloud.utils.LocalUtils;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;

public class AbleFind implements Receiver<LocalMessage> {
    private final BroadcastTask broadcastTask;
    private final ListenTask listenTask;
    protected final Receiver<ACDeviceFind> receiver;

    public AbleFind(int timeout_ms, int interval_ms, Receiver<ACDeviceFind> receiver2) {
        this.listenTask = new ListenTask(timeout_ms, interval_ms, this);
        this.broadcastTask = new BroadcastTask(timeout_ms, interval_ms, new LocalMessage(30, ByteBuffer.allocate(8).putLong(AC.majorDomainId << 16).array()));
        this.receiver = receiver2;
    }

    static ACDeviceFind parseFindDevice(byte[] deviceInfo, byte version) {
        byte[] majorDomain;
        byte[] subDomain;
        String physicalDeviceId;
        ByteBuffer byteBuffer = ByteBuffer.wrap(deviceInfo);
        String ipAddress = "";
        try {
            ipAddress = InetAddress.getByAddress(LocalUtils.iterateBytes(byteBuffer, 4)).getHostAddress();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        int lanMode = 0;
        int securityMode = -1;
        if (version == 1) {
            physicalDeviceId = new String(LocalUtils.iterateBytes(byteBuffer, 16));
            majorDomain = LocalUtils.iterateBytes(byteBuffer, 6);
            subDomain = LocalUtils.iterateBytes(byteBuffer, 2);
        } else {
            majorDomain = LocalUtils.iterateBytes(byteBuffer, 6);
            subDomain = LocalUtils.iterateBytes(byteBuffer, 2);
            if (version > 4) {
                lanMode = byteBuffer.get();
                securityMode = byteBuffer.get();
                byteBuffer.get();
                byteBuffer.get();
            }
            physicalDeviceId = new String(LocalUtils.iterateBytes(byteBuffer, byteBuffer.remaining()));
        }
        long majorDomainId = LocalUtils.parseLong(majorDomain);
        long subDomainId = LocalUtils.parseLong(subDomain);
        if (isValid(majorDomainId)) {
            return new ACDeviceFind(subDomainId, ipAddress, physicalDeviceId, lanMode, securityMode);
        }
        return null;
    }

    static boolean isValid(long mainDomainId) {
        return mainDomainId == 0 || mainDomainId == AC.majorDomainId;
    }

    public void execute(VoidCallback callback) {
        this.listenTask.execute(callback);
        this.broadcastTask.execute(callback);
    }

    public void onReceive(LocalMessage message) {
        ACDeviceFind localDevice;
        if (message.msgCode == 31 && (localDevice = parseFindDevice(message.payload, message.version)) != null && this.receiver != null) {
            this.receiver.onReceive(localDevice);
        }
    }

    public void cancel() {
        this.listenTask.cancel();
        this.broadcastTask.cancel();
    }
}
